package zcw.com.lib_leet_code;

import java.util.Arrays;

/**
 * Created by 朱城委 on 2021/7/15.<br><br>
 *
 * 键盘行
 */
public class Topic500 {

    public static void main(String[] args) {
        Topic500 instance = new Topic500();

        String[] array = new String[] {"Hello","Alaska","Dad","Peace"};
        System.out.println(Arrays.asList(instance.findWords(array)));

        array = new String[] {"omk"};
        System.out.println(Arrays.asList(instance.findWords(array)));

        array = new String[] {"adsdf","sfd"};
        System.out.println(Arrays.asList(instance.findWords(array)));
    }

    public String[] findWords(String[] words) {
        String keyboardQ = "qwertyuiop";
        String keyboardA = "asdfghjkl";
        String keyboardZ = "zxcvbnm";

        String[] result = new String[words.length];
        int counter = 0;
        for(String word : words) {
            char[] array = word.toLowerCase().toCharArray();
            String target = keyboardQ;
            if(keyboardA.indexOf(array[0]) >= 0) {
                target = keyboardA;
            }
            else if(keyboardZ.indexOf(array[0]) >= 0) {
                target = keyboardZ;
            }

            int i = 0;
            for(; i < array.length; i++) {
                if(target.indexOf(array[i]) < 0) {
                    break;
                }
            }

            if(i == array.length) {
                result[counter++] = word;
            }
        }

        return Arrays.copyOf(result, counter);
    }
}
